package com.edu.whty.controller;

import com.edu.whty.common.constants.CodeConstants;
import com.edu.whty.common.tools.UUIDUtils;
import com.edu.whty.entity.base.ResponseEntity;
import com.edu.whty.entity.share.ResShare;
import com.edu.whty.service.ResShareService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author luyao
 * @explain
 * @date 2021/8/19
 */
@Controller
@RequestMapping("/share")

public class ShareController {

    private Logger LOGGER = LogManager.getLogger(ShareController.class);

    @Autowired
    private ResShareService resShareService;

    @RequestMapping(value = "/addShare", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity addShare(@RequestBody ResShare resShare) {
        if(StringUtils.isEmpty(resShare.getUserId())
                ||StringUtils.isEmpty(resShare.getShareType())||StringUtils.isEmpty(resShare.getRealName())
               ||StringUtils.isEmpty(resShare.getDownloadUrl())||StringUtils.isEmpty(resShare.getTitle())){
            return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "必传参数不能为空");
        }
        try {
            String [] shareUserIdArr = resShare.getShareUserId().split(",");
            String [] shareNameArr = resShare.getShareUserName().split(",");
            if(shareUserIdArr.length != shareNameArr.length){
                return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "传参不规范");
            }
            List<ResShare> list = new ArrayList<>();
            for(int i=0;i<shareUserIdArr.length;i++){
                ResShare resShare0 = new ResShare();
                resShare0.setUserId(resShare.getUserId());
                resShare0.setRealName(resShare.getRealName());
                resShare0.setPreviewUrl(resShare.getPreviewUrl());
                resShare0.setDownloadUrl(resShare.getDownloadUrl());
                resShare0.setShareId(UUIDUtils.getRandomUUID());
                resShare0.setShareUserId(shareUserIdArr[i]);
                resShare0.setShareUserName(shareNameArr[i]);
                resShare0.setResId(resShare.getResId());
                resShare0.setFid(resShare.getFid());
                resShare0.setIsCloud(resShare.getIsCloud());
                resShare0.setFileMd5(resShare.getFileMd5());
                resShare0.setTitle(resShare.getTitle());
                resShare0.setShareType(resShare.getShareType());
                resShare0.setFileLength(resShare.getFileLength() != null ? resShare.getFileLength() : 0L);
                list.add(resShare0);
            }
            resShareService.addResShareBatch(list);
            return ResponseEntity.getSuccessResponse(list.get(0).getShareId());
        } catch (Exception e) {
            LOGGER.error("分享失败" + e.getMessage(), e);
            return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "分享失败");
        }
    }


    @RequestMapping(value = "/getShareList", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity getShareList(@RequestBody ResShare resShare) {
        if((StringUtils.isEmpty(resShare.getUserId())&&StringUtils.isEmpty(resShare.getShareUserId()))
                ||StringUtils.isEmpty(resShare.getShareType())){
            return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "必传参数不能为空");
        }

        try {
            PageHelper.startPage(resShare.getCurPage(), resShare.getNumPerPage());
            List<ResShare> list = resShareService.getResShareList(resShare);
            PageInfo<ResShare> pageInfo = new PageInfo<>(list);
            return ResponseEntity.getSuccessResponse(pageInfo);
        } catch (Exception e) {
            LOGGER.error("分享失败" + e.getMessage(), e);
            return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "分享失败");
        }
    }

    @RequestMapping(value = "/delShare", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity delShare(@RequestBody ResShare resShare) {
        if(StringUtils.isEmpty(resShare.getShareId())){
            return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "必传参数不能为空");
        }
        try {
            resShareService.deleteResShare(resShare);
            return ResponseEntity.getSuccessResponse();
        } catch (Exception e) {
            LOGGER.error("取消分享失败" + e.getMessage(), e);
            return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "取消分享");
        }
    }

}
